package com.bluesmart.mia;

import java.util.ArrayList;

import kankan.wheel.widget.OnWheelChangedListener;
import kankan.wheel.widget.WheelView;
import kankan.wheel.widget.adapters.ArrayWheelAdapter;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.sys.DemoApp;
import com.util.Constant;
import com.util.MgqDataHandler;
import com.util.MgqRestClient;
import com.util.PreferencesUtils;
import com.util.ToastUtil;

import android.os.Bundle;
import android.content.Intent;
import android.graphics.Color;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class BabyProfileBottleActivity extends BaseActivity implements OnClickListener{

	String[] mTime = null;
	JSONArray mArray = null;
	
	int mSelecte0 = 0;
	int mSelecte1 = 0;
	int mSelecte2 = 0;
	int mSelecte3 = 0;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_baby_profile_bottle);

		((TextView) findViewById(R.id.center)).setText(getString(R.string.bottle0));
		((Button) findViewById(R.id.right)).setCompoundDrawablesWithIntrinsicBounds(0, 0,R.drawable.a01, 0);
		
		findViewById(R.id.left).setOnClickListener(this);
		findViewById(R.id.right).setOnClickListener(this);

		((WheelView)findViewById(R.id.pick0)).addChangingListener(new OnWheelChangedListener(){
			@Override
			public void onChanged(WheelView wheel, int oldValue, int newValue) {
				mSelecte0 = newValue;
				mSelecte1 = 0;
				mSelecte2 = 0;
				mSelecte3 = 0;
				resetWheel(1);
				
				((WheelView)findViewById(R.id.pick1)).setCurrentItem(0);
				((WheelView)findViewById(R.id.pick2)).setCurrentItem(0);
				((WheelView)findViewById(R.id.pick3)).setCurrentItem(0);
			}
		});
		((WheelView)findViewById(R.id.pick1)).addChangingListener(new OnWheelChangedListener(){
			@Override
			public void onChanged(WheelView wheel, int oldValue, int newValue) {
				mSelecte1 = newValue;
				mSelecte2 = 0;
				mSelecte3 = 0;
				((WheelView)findViewById(R.id.pick2)).setCurrentItem(0);
				((WheelView)findViewById(R.id.pick3)).setCurrentItem(0);
				resetWheel(2);
			}
		});
		((WheelView)findViewById(R.id.pick2)).addChangingListener(new OnWheelChangedListener(){
			@Override
			public void onChanged(WheelView wheel, int oldValue, int newValue) {
				mSelecte2 = newValue;
				mSelecte3 = 0;
				((WheelView)findViewById(R.id.pick3)).setCurrentItem(0);
				resetWheel(2);
			}
		});
		((WheelView)findViewById(R.id.pick3)).addChangingListener(new OnWheelChangedListener(){
			@Override
			public void onChanged(WheelView wheel, int oldValue, int newValue) {
				mSelecte3 = newValue;
			}
		});
		
		sendHttpRequest();
	}

	void resetWheel(int select)
	{
		if(select == 1)
		{
			JSONObject obj = mArray.getJSONObject(mSelecte0);
			JSONArray array = obj.getJSONArray("bottlevollist");
			String[]value = new String[array.size()];
			for(int i = 0; i < array.size(); i++)
			{
				value[i] = array.getJSONObject(i).getString("bottlevol");
			}
			
			WheelView number = (WheelView)findViewById(R.id.pick1);
	        number.setViewAdapter(new ArrayWheelAdapter<String>(this, value,Color.parseColor("#FFFFFF")));
		}
		
		if(select == 1 || select == 2)
		{
			JSONObject obj = mArray.getJSONObject(mSelecte0).getJSONArray("bottlevollist").getJSONObject(mSelecte1);
			JSONArray array = obj.getJSONArray("materiallist");
			String[]value = new String[array.size()];
			for(int i = 0; i < array.size(); i++)
			{
				value[i] = array.getJSONObject(i).getString("materia");
			}
			
			WheelView number = (WheelView)findViewById(R.id.pick2);
	        number.setViewAdapter(new ArrayWheelAdapter<String>(this, value,Color.parseColor("#FFFFFF")));
		}
		
		JSONObject obj = mArray.getJSONObject(mSelecte0).getJSONArray("bottlevollist").getJSONObject(mSelecte1)
				.getJSONArray("materiallist").getJSONObject(mSelecte2);
		JSONArray array = obj.getJSONArray("caliberlist");
		String[]value = new String[array.size()];
		for(int i = 0; i < array.size(); i++)
		{
			value[i] = array.getJSONObject(i).getString("caliber");
		}
		
		WheelView number = (WheelView)findViewById(R.id.pick3);
        number.setViewAdapter(new ArrayWheelAdapter<String>(this, value,Color.parseColor("#FFFFFF")));
	}
	
	@Override
	public void onClick(View arg0) {
		if(R.id.left == arg0.getId()) finish();
		else
		{
			if(mArray == null || mArray.size() == 0) return;
			
			String name = mArray.getJSONObject(mSelecte0).getString("brand") 
					+ mArray.getJSONObject(mSelecte0).getJSONArray("bottlevollist").getJSONObject(mSelecte1).getString("bottlevol")
					+ mArray.getJSONObject(mSelecte0).getJSONArray("bottlevollist").getJSONObject(mSelecte1)
					.getJSONArray("materiallist").getJSONObject(mSelecte2).getString("materia")
					+ mArray.getJSONObject(mSelecte0).getJSONArray("bottlevollist").getJSONObject(mSelecte1)
					.getJSONArray("materiallist").getJSONObject(mSelecte2).getJSONArray("caliberlist").getJSONObject(mSelecte3).getString("caliber");
			String milkId = mArray.getJSONObject(mSelecte0).getJSONArray("bottlevollist").getJSONObject(mSelecte1)
					.getJSONArray("materiallist").getJSONObject(mSelecte2).getJSONArray("caliberlist").getJSONObject(mSelecte3).getString("bottleId");
			
			Intent intent = new Intent();
			intent.putExtra("typeId", milkId);
			intent.putExtra("value",  name);
			this.setResult(RESULT_OK, intent);

			sendRequest(milkId);
		}
	}
	
	private void sendRequest(String milkId) {
		MgqDataHandler loginHandler = new MgqDataHandler(this, true, false) {
			@Override
			public void onSuccess(String response) {

				JSONObject ruser = JSON.parseObject(response);

				if (ruser.getString("resCode").equals("1000")) {
					finish();
					ToastUtil.showToastWithAlertPic(R.string.success);
				}
				else if (ruser.getString("resCode").equals("2000")) {
					ToastUtil.showToastWithAlertPic(R.string.error_2000);
				}
				else if (ruser.getString("resCode").equals("3000")) {
					ToastUtil.showToastWithAlertPic(R.string.error_3000);
				}
				else 
				{
				}
			}
		};
		
		JSONObject obj = ((DemoApp)getApplicationContext()).getSeletedBaby();
		
		String username = PreferencesUtils.getString(this, "username");
		String password = PreferencesUtils.getString(this, "password");
		if(username == null || username.length() == 0)
		{
			username = PreferencesUtils.getString(this, "username_tmp");
			password = PreferencesUtils.getString(this, "password_tmp");
		}

		JSONObject params = new JSONObject();
		params.put("mia_un", username);
		params.put("pwd", password);
		params.put("babyid", obj.getString("babyid"));
		params.put("handType", "addpreferences");
		params.put("bottleid", milkId);
		params.put("milkid", getIntent().getStringExtra("id"));
		params.put("drinktemp", getIntent().getStringExtra("tmp"));
		
		MgqRestClient.postBody(Constant.SERVER_URL + "/sw/babyInfoHandler.ashx", JSON.toJSONString(params), loginHandler);
	}
	
	private void sendHttpRequest() {
		MgqDataHandler loginHandler = new MgqDataHandler(this, true, false) {
			@Override
			public void onSuccess(String response) {
				mArray = JSON.parseArray(response);
				if(mArray != null)
				{
					mTime = new String[mArray.size()];
					for(int i = 0; i < mArray.size(); i++)
					{
						mTime[i] = mArray.getJSONObject(i).getString("brand");
					}
					
					WheelView number = (WheelView)findViewById(R.id.pick0);
			        number.setViewAdapter(new ArrayWheelAdapter<String>(BabyProfileBottleActivity.this, mTime,Color.parseColor("#FFFFFF")));
					
					resetWheel(1);
				}
			}
		};
		
		String username = PreferencesUtils.getString(this, "username");
		String password = PreferencesUtils.getString(this, "password");
		if(username == null || username.length() == 0)
		{
			username = PreferencesUtils.getString(this, "username_tmp");
			password = PreferencesUtils.getString(this, "password_tmp");
		}
		
		JSONObject params = new JSONObject();
		params.put("mia_un", username);
		params.put("pwd", password);
		params.put("handType", "bottle");

		MgqRestClient.postBody(Constant.SERVER_URL + "/sw/BasicData.ashx", JSON.toJSONString(params), loginHandler);
	}
}
